﻿using DiffuseEnjoyIdle.MessageCenter.RabbitMQ;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace DiffuseEnjoyIdle.RabbitMQ
{
    public class MQWorker : BackgroundService
    {
        private readonly ILogger<MQWorker> _logger;
        private readonly RabbitMQClient _rabbitMqClient;

        public MQWorker(ILogger<MQWorker> logger, RabbitMQClient rabbitMQClient)
        {
            _logger = logger;
            _rabbitMqClient = rabbitMQClient;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation("MQWorker Start");

            await _rabbitMqClient.StartAsync();

            while (!stoppingToken.IsCancellationRequested)
            {
                await Task.Delay(200, stoppingToken);
            }

            await _rabbitMqClient.StopAsync();
            _logger.LogInformation("MQWorker Stop");
        }
    }

}
